package com.woniu.liuzhougov.itemAndTask.service.impl;

import com.woniu.liuzhougov.authorityControl.pojo.User;
import com.woniu.liuzhougov.itemAndTask.dao.DepartmentMapper;
import com.woniu.liuzhougov.itemAndTask.dao.ItemTaskMapper;
import com.woniu.liuzhougov.itemAndTask.dao.ItemsMapper;
import com.woniu.liuzhougov.itemAndTask.pojo.Department;
import com.woniu.liuzhougov.itemAndTask.pojo.ItemsPojo;
import com.woniu.liuzhougov.itemAndTask.service.ItemsManageService;
import com.woniu.liuzhougov.itemAndTask.vo.AddItemVo;
import com.woniu.liuzhougov.itemAndTask.vo.ItemVo;
import com.woniu.liuzhougov.report.dao.AwaitingApprovalReportMapper;
import com.woniu.liuzhougov.report.vo.InsertReportVo;
import com.woniu.liuzhougov.systemSettings.dao.LogMapper;
import com.woniu.liuzhougov.systemSettings.pojo.Log;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Transactional(rollbackFor = Exception.class)
@Service
public class ItemsManageServiceImpl implements ItemsManageService {


    @Autowired
    private ItemsMapper itemsMapper;
    @Autowired
    private ItemTaskMapper itemTaskMapper;
    @Autowired
    private AwaitingApprovalReportMapper reportMapper;
    @Autowired
    private DepartmentMapper departmentMapper;
    @Autowired
    private LogMapper logMapper;

    /**
     * 分页查看所有 区级 事项
     * @return
     */
    @Override
    public List<ItemsPojo> allItms(Integer userOfficeId, Integer userAddrId) {
        List<ItemsPojo> list = itemsMapper.listItems(userAddrId, userOfficeId);
        return list;
    }

    /**
     * 分页查看所有 市级 事项
     * @return
     */
    @Override
    public List<ItemsPojo> allSItms(Integer departmentId, Integer areaId) {
        List<ItemsPojo> list = itemsMapper.listSItems(areaId, departmentId);
        return list;
    }

    /**
     * 查询所有  我的事项
     * @param userId
     * @return
     */
    @Override
    public List<ItemsPojo> listMyItems(Integer userId) {
        return itemsMapper.listMyItems(userId);
    }


    /**
     * 查询所有 待审批事项
     * @param departmentId
     * @return
     */
    @Override
    public List<ItemsPojo> listAwaitingApprovalItems(Integer departmentId, Integer areaId) {
        return itemsMapper.listAwaitingApprovalItems(departmentId, areaId);
    }


    /**
     * 删除事项
     * @param sxId 事项ID
     * @return
     */
    @Override
    public String deleteItemBySxID(Integer sxId) {
        int count;
        int num = itemTaskMapper.countTaskState(sxId);
        if (num == 0) {
            itemTaskMapper.setFlagToOne(sxId);
            count = itemsMapper.updateFlagByItemId(sxId);
        } else {
            return "hasTask";
        }
        if (count > 0) {
            Log log = new Log();
            User user = (User) SecurityUtils.getSubject().getPrincipal();
            log.setItemId(sxId);
            log.setLogOperation("删除一个事项！");
            log.setUserId(user.getUserId());
            logMapper.insertSelective(log);
            return "success";
        }
        return "loss";
    }


    /**
     * 添加事项
     * @param vo
     * @return
     */
    @Override
    public boolean insertNewItem(AddItemVo vo) {
        //日志添加
        Log log = new Log();
        User user = (User) SecurityUtils.getSubject().getPrincipal();
        log.setItemId(vo.getItemId());
        log.setLogOperation("添加一个事项！");
        log.setUserId(user.getUserId());

        //事项添加
        Integer urgency = vo.getItemUrgency();
        int count;
        int itemId;
        int number;

        if(urgency == 1){
            count = itemsMapper.insertNewItem(vo);
            if (count > 0) {
                logMapper.insertSelective(log);
                return true;
            }
        }

        if(urgency == 2){
            //添加专报
            Department department = departmentMapper.getLeaderId(vo.getDepartmentId());
            if(department.getLeader() == null){
                return false;
            }
            //添加事项
            count = itemsMapper.insertNewItem(vo);
            itemId = vo.getItemId();

            InsertReportVo insertReportVo = new InsertReportVo();
            insertReportVo.setReportStateId(1);
            insertReportVo.setItemId(itemId);
            insertReportVo.setReportContent("事项《" + vo.getItemTitle() + "》相关");
            insertReportVo.setReportFounderId(vo.getItemFounderId());
            insertReportVo.setReportRecenderId(department.getLeader().getUserId());

            number = reportMapper.insertReport(insertReportVo);
            if (count > 0 && number > 0) {
                logMapper.insertSelective(log);
                return true;
            }
        }
        return false;
    }

    /**
     * 通过Id 查询事项
     * @param itemId
     * @return
     */
    @Override
    public ItemsPojo searchItemByItemId(Integer itemId) {
        return itemsMapper.searchById(itemId);
    }


    /**
     * 修改事项
     * @param editItemContent
     * @param editItemRemark
     * @param editItemTitle
     * @param editItemUrgency
     * @param itemId
     * @return
     */
    @Override
    public boolean updateItemById(String editItemContent, String editItemRemark, String editItemTitle, Integer editItemUrgency, Integer itemId) {
        int count = itemsMapper.updateItemById(itemId, editItemTitle, editItemUrgency, editItemContent, editItemRemark);


        Log log = new Log();
        User user = (User) SecurityUtils.getSubject().getPrincipal();
        log.setItemId(itemId);
        log.setLogOperation("修改一个事项！");
        log.setUserId(user.getUserId());

        /* 若事项从 正常 修改为 紧急，则建立专报 */
        if (editItemUrgency == 2) {

            ItemVo itemVo = new ItemVo();
            itemVo = itemsMapper.selectVo(itemId);

            InsertReportVo insertReportVo = new InsertReportVo();

            insertReportVo.setReportStateId(1);
            insertReportVo.setItemId(itemId);
            insertReportVo.setReportContent("事项《" + editItemTitle + "》相关");
            insertReportVo.setReportFounderId(itemVo.getUserId());
            insertReportVo.setReportRecenderId(itemVo.getDepartment().getLeader().getUserId());


            int num = reportMapper.insertReport(insertReportVo);
            if (num > 0 && count > 0) {
                logMapper.insertSelective(log);
                return true;
            } else {
                return false;
            }
        }
        if (count > 0) {
            logMapper.insertSelective(log);
            return true;
        }
        return false;
    }
}
